package main.java.com.web;

import main.java.com.service.DishService;
import main.java.com.service.impl.DishServiceImpl;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

//修改禁用和启用页面
@WebServlet("/UpdateDishStatusServlet")
public class UpdateDishStatusServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //编码格式
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=UTF-8");

        // 获取请求参数中的id，并转换为Long类型
        Long id = Long.parseLong(request.getParameter("id"));
        // 获取请求参数中的status，并转换为Integer类型
        String statusStr = request.getParameter("status");
        Integer status = Integer.parseInt(statusStr);

        // 创建DishService实例，用于后续调用菜品状态更新方法
        DishService dishService = new DishServiceImpl();
        // 调用updateDishStatus方法更新菜品状态，参数包括菜品id和新的状态码
        dishService.updateDishStatus(id, status);


        // 设置响应状态码为200（表示成功）
        response.setStatus(HttpServletResponse.SC_OK);
        // 返回特定的响应内容
        response.getWriter().write("success");

        // 记录日志
        System.out.println("Update dish status: id=" + id + ", status=" + status);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
